<template>
  <div class="search">
    <div class="search-container">
      <el-icon class="search-icon" @click="onSearch"><Search /></el-icon>
      <el-input
        type="text"
        v-model="searchQuery"
        placeholder="搜索..."
        class="search-input"
        @focus="focus = true"
        @blur="focus = false"
        @keyup.enter="onSearch"
      />
    </div>
  </div>
</template>

<script setup>
import { defineEmits, ref } from 'vue'
import { Search } from '@element-plus/icons-vue' // 引入 Search 图标组件
import { useRouter } from 'vue-router'

const props = defineProps({
  search: {
    type: String,
    default: '',
  },
})

const router = useRouter()

const emit = defineEmits(['search'])

// 搜索内容
const searchQuery = ref(props.search || '')

// 聚焦
const focus = ref('')

// 搜索
const onSearch = () => {
  if (searchQuery.value === '') {
    return
  }
  router.push({ path: `/search/${searchQuery.value}` })
  emit('search', searchQuery.value)
}
</script>

<style scoped less="scss">
.search {
  position: relative;

  .search-container {
    display: flex;
    justify-content: center;
    align-items: center; /* 垂直对齐 */

    position: relative;
    width: 500px;
    margin-top: 15px;
    overflow: hidden;

    .search-icon {
      position: absolute;
      right: 2%;
      z-index: 1;
      transition: all 0.5s;
    }

    .search-icon:hover {
      color: #0190c0;
      cursor: pointer;
    }

    .search-input {
      height: 35px; /* 设置与按钮一致的高度 */
      width: 500px; /* 输入框占据剩余空间 */
      font-size: 18px; /* 调整字体大小 */
    }
  }
}
</style>
